package com.zy.project.system.role.entity.base;

import com.alibaba.fastjson.annotation.JSONField;
import com.zy.core.data.jpa.support.entity.BaseEntity;
import com.zy.project.system.permission.entity.SystemPermission;
import com.zy.project.system.user.entity.SystemUser;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * 系统角色
 * Package: core.entity
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-9-1 22:17
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */

@Getter
@Setter
@MappedSuperclass
public class BaseSystemRole extends BaseEntity {

    /**
     * 主键
     */
    @Id
    @GenericGenerator(name ="jpa-uuid", strategy = "uuid")
    @GeneratedValue(generator = "jpa-uuid")
    @Column(name = "id")
    private String id;

    /**
     * 角色名称
     */
    @Column(name = "role_name")
    private String roleName;

    /**
     * 创建时间
     */
    @Column(name = "create_date")
    private Date createDate;

    /**
     * 更新时间
     */
    @Column(name = "update_date")
    private Date updateDate;


    /**
     * 描述
     */
    @Column(name = "description")
    private String description;

    /**
     * 角色编码
     */
    @Column(name = "code")
    private String code;

    /**
     * 角色序号
     */
//    @Column(name = "order")
//    private Integer order;

    /**
     * 角色拥有的权限
     * @JoinColumn name表示中间表栏位,referencedColumnName表示参考表栏位
     */
    @ManyToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name="system_role_permission",
            joinColumns={@JoinColumn(name="role_id",referencedColumnName="id")},
            inverseJoinColumns={@JoinColumn(name="permission_id",referencedColumnName="id")})
    @JSONField(serialize=false)
    private Set<SystemPermission> permissions;

    /**
     *角色与用户的关系
     * targetEntity 表示目标类,mappedBy目标类中的属性或者表名,关系由目标类来维护
     */
    @ManyToMany(targetEntity = SystemUser.class,
            mappedBy="roles",fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    @JSONField(serialize=false)
    private Set<SystemUser> users;

}
